package game.utils;

import game.editer.EditerStage;
#if editer
import game.editer.view.AlertView;
#end
import sys.io.File;
import game.data.ProjectDataExport;
import game.data.ProjectRoleData;
import haxe.io.Path;

/**
 * 项目工具类
 */
class ProjectTools {
	public static var hxmlContent = "--library anime-battle-next
--class-path src
--main Script
--no-output
--js bin/main.js
--library lime
--library openfl
--library hxmaker-openfl
--library hxmaker
--library actuate
--library spine-haxe
--library tweenxcore
--library openfl-glsl
--library vector-math
--library differ
--library hscript
--library echo
--macro echo.Macros.add_data('entity', 'game.worlds.IDisplay')
";

	public static var scriptCodeContent = "package;

import game.script.RoleScript;

class Script extends RoleScript {
	static function main() {}

	override function onInit() {
		super.onInit();
		// Source code
	}
}
";

	public static var gitigonreContent = "/dist
.DS_Store";

	/**
	 * 创建项目，会自动产生images、sound文件夹
	 * @param path 
	 */
	public static function createProject(path:String):String {
		var dir = Path.directory(path);
		var file = Path.withoutExtension(path) + ".abproj";
		// 创建gitignore文件
		File.saveContent(Path.join([dir, ".gitignore"]), gitigonreContent);
		FileSystem.createDirectory(Path.join([dir, "images"]));
		FileSystem.createDirectory(Path.join([dir, "sound"]));
		var project = new ProjectRoleData();
		var export = new ProjectDataExport(project);
		File.saveContent(file, export.export());
		return file;
	}

	/**
	 * 创建HAXE代码，会自动生成hx文件，hxml编译配置文件
	 * @param path 
	 */
	public static function createHaxeProject(path:String, force:Bool = false):Void {
		var dir = Path.directory(path);
		var src = Path.join([dir, "src"]);
		FileSystem.createDirectory(src);
		var exist = sys.FileSystem.exists(Path.join([src, "Script.hx"]));
		if (!exist || force) {
			// 创建hx文件
			File.saveContent(Path.join([src, "Script.hx"]), scriptCodeContent);
			// 创建hxml文件
			File.saveContent(Path.join([dir, "build.hxml"]), hxmlContent);
			#if editer
			AlertView.showView("提示", "代码初始化完毕");
			#end
		} else {
			#if editer
			AlertView.showView("初始化代码", "已经存在角色脚本，是否覆盖？", ["取消", "确定"], (tag) -> {
				if (tag == "确定") {
					createHaxeProject(path, true);
				}
			});
			#end
		}
	}
}
